core: Threadsafe recursive direction creation is harder than it appears
authorColin Walters <walters@verbum.org>
Fri, 12 Oct 2012 21:44:02 +0000 (17:44 -0400)
committerColin Walters <walters@verbum.org>
Sat, 13 Oct 2012 14:47:03 +0000 (10:47 -0400)
src/libotutil/ot-gio-utils.c

index bf3743d32ab01847240f12b4a3e99b0a54486b50..e06eebeeff6e2b79f42c6694496304dbe745c7b0 100644 (file)
@@ -57,19 +57,27 @@ ot_gfile_ensure_directory (GFile     *dir,
   gboolean ret = FALSE;
   GError *temp_error = NULL;
 
-  if (with_parents)
+  if (!g_file_make_directory (dir, NULL, &temp_error))
     {
-      ot_lobj GFile *parent = g_file_get_parent (dir);
-      if (parent)
-        ret = ot_gfile_ensure_directory (parent, TRUE, &temp_error);
-      else
-        ret = TRUE;
-    }
-  else
-    ret = g_file_make_directory (dir, NULL, &temp_error);
-  if (!ret)
-    {
-      if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+      if (with_parents &&
+          g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          ot_lobj GFile *parent = NULL;
+
+          g_clear_error (&temp_error);
+
+          parent = g_file_get_parent (dir);
+          if (parent)
+            {
+              if (!ot_gfile_ensure_directory (parent, TRUE, error))
+                goto out;
+              if (!g_file_make_directory (dir, NULL, error))
+                goto out;
+            }
+          else
+            g_assert_not_reached ();
+        }
+      else if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
         {
           g_propagate_error (error, temp_error);
           goto out;